/**
 * @name 数据库stu_h1802的路由操作
 * @author 杨标
 * @version 1.0
 * @description 路由
 */

const express=require("express"),
    Stu_h1802Service=require("../services/Stu_h1802Service"),
    MessageBox=require("../utils/MessageBox"),
    Stu_h1802=require("../model/Stu_h1802"),
    PageJson=require("../model/PageJson");

let router=express.Router();

router.get("/login",(req,resp)=>{
    resp.render("stu_h1802/login");   
});

//检测登陆
router.post("/checkLogin",async (req,resp)=>{
    let param ={s_id,s_qq}=req.body;
    let stu_h1802Service=new Stu_h1802Service();
    try {
        let result = await stu_h1802Service.checkLogin(param);
        if(result.length==1){
            //在这个地方，说明登陆成功了，
            //登陆成功以后，我创建一个“保险箱”，来存放你的信息，用以标识，你已经登陆了
            req.session.stuInfo=result[0];
           MessageBox.showAndRedirect("登陆成功","/adminIndex",resp);
        }
        else{
            MessageBox.showAndBack("用户名或密码失败",resp);
        }
    } catch (error) {
        MessageBox.showAndBack("服务器错误",resp);
    }
});

router.get("/stuList",async (req,resp)=>{
    let stu_h1802Service =new Stu_h1802Service();
    let pageIndex=req.query.pageIndex||1;
    try {
        let pageList=await stu_h1802Service.queryByList(req.query,pageIndex);
        resp.render("stu_h1802/stuList",pageList);
    } catch (error) {
        resp.send("服务器错误");
        console.log(error);
    }
});


router.get("/queryByList",async (req,resp)=>{
    //在这里我们就得到了后面提交过来的参数
    //根据这三个参数到后台去查询
    let stu_h1802Service =new Stu_h1802Service();
    let pageIndex=req.query.pageIndex||1;
    try {
        let pageList=await stu_h1802Service.queryByList(req.query,pageIndex);
        resp.render("stu_h1802/stuList",pageList);
    } catch (error) {
        resp.send("服务器错误");
        console.log(error);
    }
});

router.get("/addStudent",(req,resp)=>{
    let stu_h1802=new Stu_h1802();
    let objArr=Array.from(Object.entries(stu_h1802));
    resp.render("stu_h1802/addStudent",{objArr:objArr});
});

router.post("/doAddStudent", async (req,resp)=>{
    //get取值的时候找req.query
    //post取值找req.body
    //默认情况下，Express不支持post取值，需要使用第三方插件（包）
    //yarn add body-parser
    let stu_h1802Service=new Stu_h1802Service();
    try {
        let result=await stu_h1802Service.addStudent(req.body);
        if(result.affectedRows>0){
            MessageBox.showAndRedirect("新增成功","/Stu_h1802/stuList",resp);
        }
        else{
            MessageBox.showAndBack("新增失败",resp);
        }
    } catch (error) {
        MessageBox.showAndBack("服务器错误",resp);
    }
});

//Ajax请求
router.get("/deleteById",async (req,resp)=>{
    let {s_id}=req.query;
    try {
        let stu_h1802Service=new Stu_h1802Service();
        let flag=await stu_h1802Service.deleteById(s_id);
        if(flag){
            //说明删除成功
            let pageJson=new PageJson("success","删除成功");
            let jsonStr=JSON.stringify(pageJson);
            resp.send(jsonStr);
        }
        else{
            //删除失败
            let pageJson=new PageJson("error","删除失败");
            let jsonStr=JSON.stringify(pageJson);
            resp.send(jsonStr);
            
        }
    } catch (error) {
        //服务器错误
        let pageJson=new PageJson("error","肥务器错误");
        let jsonStr=JSON.stringify(pageJson);
        resp.send(jsonStr);
    }
});

//选除选中项（批量删除）
router.get("/deleteByChecked",async (req,resp)=>{
    let {s_idArr}=req.query;
    //dddda-H18020001-H18020002
    let arr=s_idArr.split("-");   //根据指定的符号去分隔字成串，把它分成一个数组  ["dddda","H18020001","H18020002"]
    try {
        let stu_h1802Service=new Stu_h1802Service();
        let flag=await stu_h1802Service.deleteByChecked(arr);
        if(flag){
            //说明删除成功
            let pageJson=new PageJson("success","删除成功");
            // let jsonStr=JSON.stringify(pageJson);
            // resp.send(jsonStr);
            resp.json(pageJson);
        }
        else{
            //删除失败
            let pageJson=new PageJson("error","删除失败");    //对象
            //let jsonStr=JSON.stringify(pageJson);    //将JS对象转成JSON字符串
            //resp.send(jsonStr);  //发送一个字符串
            //Express可以直接将一个对象以JSON形式传递到前台
            resp.json(pageJson);     //直接以对象的形式传递   前台接收到的时候，就不用再进行转换了，得到的，也是一个对象
            
        }
    } catch (error) {
        //服务器错误
        console.log(error);
        let pageJson=new PageJson("error","服务器错误");            
        // let jsonStr=JSON.stringify(pageJson);
        // resp.send(jsonStr);
        resp.json(pageJson);
    }
});



router.get("/editStudent",async (req,resp)=>{
    let {s_id}=req.query;   //根据学号取得数据库的信息
    try {
        let stu_h1802Service=new Stu_h1802Service();
        let result=await stu_h1802Service.findByS_id(s_id);
        if(result.length==1){
            resp.render("stu_h1802/editStudent",{model:result[0]});
        }
        else{
            MessageBox.showAndBack("当前信息不存在，请刷新重试",resp);
        }
    } catch (error) {
        MessageBox.showAndBack("服务器错误",resp);
    }
});


router.post("/doEditStudent",async (req,resp)=>{
    try {
        let stu_h1802Service=new Stu_h1802Service();
        let flag=await stu_h1802Service.editStudent(req.body);
        if(flag){
            //修改成功
            MessageBox.showAndRedirect("修改成功","/Stu_h1802/stuList",resp);
        }
        else{
            //修改失败
            MessageBox.showAndBack("修改失败，请重试或联系管理员",resp);
        }
    } catch (error) {
        MessageBox.showAndBack("服务器错误",resp);
    }
});

//退出登陆
router.get("/logOut",(req,resp)=>{
    //清空session  "保险箱"
    req.session.stuInfo=undefined;
    //退出回到登陆页面
    resp.redirect("/Stu_h1802/login");    
});



module.exports=router;